package top.wintp.juhedatademo.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import top.wintp.juhedatademo.R;


/**
 * 应用里面内置的浏览器服务
 */
public class ShowUrlActivity extends AppCompatActivity {
    private String mTitle;
    private String mUrl;
    /**
     * 动态的创建WebView  减小内存消耗
     */
    private FrameLayout mFl_web_view;
    private WebView mWebView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_show_url);

        //x5 设置键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
                | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


        mTitle = getIntent().getStringExtra("title");
        mUrl = getIntent().getStringExtra("url");
        initView();


    }

    /**
     * 初始化View
     */
    private void initView() {
        mFl_web_view = findViewById(R.id.fl_web_view);

        //初始化WebView
        initWebView();
    }


    /**
     * 初始化WebView
     */
    @SuppressLint("WrongConstant")
    private void initWebView() {
        mWebView = new WebView(this);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        mWebView.setLayoutParams(layoutParams);


        mWebView.loadUrl(mUrl);
        WebSettings settings = mWebView.getSettings();


        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);

                return true;
            }
        });


        mWebView.setWebChromeClient(new WebChromeClient() {

        });

        settings.setJavaScriptEnabled(true);

        //设置加载图片
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setDefaultTextEncodingName("utf-8");// 避免中文乱码
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        settings.setNeedInitialFocus(false);
        settings.setSupportZoom(true);
        settings.setLoadWithOverviewMode(true);//适应屏幕
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadsImagesAutomatically(true);//自动加载图片
        settings.setCacheMode(WebSettings.LOAD_DEFAULT
                | WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mFl_web_view.removeAllViews();
        mFl_web_view.addView(mWebView);
    }


    @Override
    public void onBackPressed() {

        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finish();
        }

    }

    @Override
    protected void onDestroy() {

        if (mWebView != null) {
            mWebView.destroy();
            mFl_web_view.removeView(mWebView);
            mWebView = null;
        }

        super.onDestroy();
    }
}
